import Axios from 'axios'
import qs from 'qs'
import * as ramda from 'ramda'

const apiAuthDomain = 'http://www.xxxxxx.com' 
const apiControlDomain = 'http://www.xxxxxxxxx.com' 
/**
 * 成功回调函数
 * @param {*} res
 * @param {*} resolve
 * @param {*} reject
 */
function successCallBack(res, resolve, reject) {
    if (res.data.success) {
        resolve(res)
    } else {
        reject(res)
    }
}

/**
 * 异常回调函数
 * @param {*} err 异常数据
 * @param {*} reject
 */
function failCallBack(err, reject) {
    reject(err)
}
/** Axios 基础配置 */
Axios.defaults.timeout = 5000
/**
 * http request 拦截器
 */
Axios.interceptors.request.use(
    config => {
        let tokenName = ''
        if (
            !ramda.isNil(config.params['tokenName']) &&
            !ramda.isEmpty(config.params['tokenName'])
        ) {
            // console.log(config);
            tokenName = config.params['tokenName']
        }
        if (config.params['authorization']) {
            // 两个token  谁有就拿那个
            let token = ''
            if (localStorage.getItem(tokenName) == null) {
                token = sessionStorage.getItem(tokenName)
            } else {
                token = localStorage.getItem(tokenName) // 读取数据;
            }
            config.headers['Authorization'] = token
        }
        delete config.params['authorization']
        delete config.params['tokenName']
        // config.headers["Content-Type"] = 'application/x-www-form-urlencoded';
        return config
    },
    err => {
        return Promise.reject(err)
    }
)
/**
 * http response 拦截器
 */
// Axios.interceptors.response.use(
//     response => {
//         if (response.data.errCode === "") {
//             //
//         }
//         console.log({ "axios-res": response })
//         return response;
//     },
//     err => {
//         console.log({ "axios-err": err })
//         return Promise.reject(err);
//     }
// )

/**
 * 处理请求链接,参数
 * @param { Object } paramData 参数/数据
 * @param { String } authorization Token
 */
function resolveAuthorization(paramData, authorization, tokenName) {
    if (ramda.isNil(paramData)) {
        paramData = {}
    }

    paramData.authorization = authorization
    paramData.tokenName = tokenName
    return paramData
}

/**
 * Axios Get 请求
 * @param { String } url Url
 * @param { Object } paramData 参数/数据
 * @param { String } authorization Token
 */
function Get(url, paramData, authorization = true, tokenName = 'token') {
    return new Promise((resolve, reject) => {
        Axios.get(url, {
            params: resolveAuthorization(paramData, authorization, tokenName)
        })
            .then(res => {
                successCallBack(res, resolve, reject)
            })
            .catch(err => {
                failCallBack(err, reject)
            })
    })
}

/**
 * Axios Post 请求
 * @param { String } url Url
 * @param { Object } paramData 参数/数据
 * @param { String } authorization Token
 */
function Post(url, paramData, authorization = true, tokenName = 'token') {
    return new Promise((resolve, reject) => {
        Axios.post(url, paramData, {
            params: resolveAuthorization({}, authorization, tokenName)
        })
            .then(res => {
                successCallBack(res, resolve, reject)
            })
            .catch(err => {
                failCallBack(err, reject)
            })
    })
}

/**
 * Axios Put 方法
 * @param { String } url Url
 * @param { Object } paramData 参数/数据
 * @param { String } authorization Token
 */
function Put(url, paramData, authorization = true, tokenName = 'token') {
    return new Promise((resolve, reject) => {
        Axios.put(url, paramData, {
            params: resolveAuthorization(paramData, authorization, tokenName)
        })
            .then(res => {
                successCallBack(res, resolve, reject)
            })
            .catch(err => {
                failCallBack(err, reject)
            })
    })
}

/**
 * Axios Del 方法
 * @param { String } url Url
 * @param { Object } paramData 参数/数据
 * @param { String } authorization Token
 */
function Del(url, paramData, authorization = true, tokenName = 'token') {
    return new Promise((resolve, reject) => {
        Axios.delete(url, {
            params: resolveAuthorization(paramData, authorization, tokenName)
        })
            .then(res => {
                successCallBack(res, resolve, reject)
            })
            .catch(err => {
                failCallBack(err, reject)
            })
    })
}
export { apiAuthDomain, apiControlDomain, Get, Put, Post, Del }
